<?php 
if(KT!='KhiThe') exit;
class PositionModules_controller extends PositionModules_model{
		
	public function __construct(){
		global $lang;
		$this->lang = $lang;
		$this->lang['sms'] ='';
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_string');
		parent::addObject('khithe_request');
		parent::addObject('khithe_upload');
	}
	
	public function index_action()
	{
		$resultHtmlHeader = '';
        $resultHtmlLeft = '';
        $resultHtmlMiddle = '';
        $resultHtmlRight = '';
        $resultHtmlFooter = '';
        $resultHtmlModule = '';
        $this->lang['column'] = '';
		$resultHtmlCatCom = '';
		$ComID = $this->valUrl(1)?$this->valUrl(1):1;
		$this->lang['Comid'] = $ComID;
        if($ComID=='1') $this->lang['column'] = 'column';
		$strHtmlContent = $this->khithe_display('index');
		$arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		/* Cat Com */
		$resultCatCom = parent::getComponents();
		while( $rowCatCom = mysql_fetch_assoc($resultCatCom) )
		{	
			$arrCatCom = array(
				"com_ID"=>$rowCatCom['ID'],
				"com_Value"=>$rowCatCom['ComTitle'],
				"cell_selected" => ($ComID==$rowCatCom['ID']?' selected="selected" ':''),
			);			
			$resultHtmlCatCom .= $this->khithe_assign($arrBlockHtml['category'],$arrCatCom);
		} mysql_free_result($resultCatCom);
		$this->lang['CatCom'] = $resultHtmlCatCom;
		/* End Cat Com */

        $strid = 0;
		$resultPositionModule = parent::getPositionModules($ComID);
		while( $row_PositionModules = mysql_fetch_assoc($resultPositionModule) )
		{
            $strid = $strid .','. $row_PositionModules['MID'];
			if($row_PositionModules['Position']=='header')
            {
                $arrPositionModuleHeader = array(
                    "ComID"=>$ComID,
                    "cell_ID"=>$row_PositionModules['ID'],
                    "MID"=>$row_PositionModules['MID'],
                    "cell_1"=>'<input name="delId[]" type="checkbox" value="'.$row_PositionModules['ID'].'" />',
                    "ModuleDir"=>$row_PositionModules['ModuleDir'],
                    "ModuleTitle"=>$row_PositionModules['ModuleTitle'],
                    "module_note"=>$row_PositionModules['ModuleDir'],
                    "cell_5"=>$row_PositionModules['Status']=='true'?'enabled':'disabled',
                    "class_cell"=>$row_PositionModules['Status']=='true'?"cell_groupitem1":"cell_groupitem_ex1",
                );
                $resultHtmlHeader .= $this->khithe_assign($arrBlockHtml['module1'],$arrPositionModuleHeader);
            }

            if($row_PositionModules['Position']=='left')
            {
                $arrPositionModuleLeft = array(
                    "ComID"=>$ComID,
                    "cell_ID"=>$row_PositionModules['ID'],
                    "MID"=>$row_PositionModules['MID'],
                    "cell_1"=>'<input name="delId[]" type="checkbox" value="'.$row_PositionModules['ID'].'" />',
                    "ModuleDir"=>$row_PositionModules['ModuleDir'],
                    "ModuleTitle"=>$row_PositionModules['ModuleTitle'],
                    "module_note"=>$row_PositionModules['ModuleDir'],
                    "cell_5"=>$row_PositionModules['Status']=='true'?'enabled':'disabled',
                    "class_cell"=>$row_PositionModules['Status']=='true'?"cell_groupitem1":"cell_groupitem_ex1",
                );
                $resultHtmlLeft.= $this->khithe_assign($arrBlockHtml['module1'],$arrPositionModuleLeft);
            }

            if($row_PositionModules['Position']=='right')
            {
                $arrPositionModuleRight = array(
                    "ComID"=>$ComID,
                    "cell_ID"=>$row_PositionModules['ID'],
                    "MID"=>$row_PositionModules['MID'],
                    "cell_1"=>'<input name="delId[]" type="checkbox" value="'.$row_PositionModules['ID'].'" />',
                    "ModuleDir"=>$row_PositionModules['ModuleDir'],
                    "ModuleTitle"=>$row_PositionModules['ModuleTitle'],
                    "module_note"=>$row_PositionModules['ModuleDir'],
                    "cell_5"=>$row_PositionModules['Status']=='true'?'enabled':'disabled',
                    "class_cell"=>$row_PositionModules['Status']=='true'?"cell_groupitem1":"cell_groupitem_ex1",
                );
                $resultHtmlRight .= $this->khithe_assign($arrBlockHtml['module1'],$arrPositionModuleRight);
            }

            if($row_PositionModules['Position']=='middle')
            {
                $arrPositionModuleMiddle = array(
                    "ComID"=>$ComID,
                    "cell_ID"=>$row_PositionModules['ID'],
                    "MID"=>$row_PositionModules['MID'],
                    "cell_1"=>'<input name="delId[]" type="checkbox" value="'.$row_PositionModules['ID'].'" />',
                    "ModuleDir"=>$row_PositionModules['ModuleDir'],
                    "ModuleTitle"=>$row_PositionModules['ModuleTitle'],
                    "module_note"=>$row_PositionModules['ModuleDir'],
                    "cell_5"=>$row_PositionModules['Status']=='true'?'enabled':'disabled',
                    "class_cell"=>$row_PositionModules['Status']=='true'?"cell_groupitem1":"cell_groupitem_ex1",
                );
                $resultHtmlMiddle.= $this->khithe_assign($arrBlockHtml['module1'],$arrPositionModuleMiddle);
            }

            if($row_PositionModules['Position']=='footer')
            {
                $arrPositionModuleFooter = array(
                    "ComID"=>$ComID,
                    "cell_ID"=>$row_PositionModules['ID'],
                    "MID"=>$row_PositionModules['MID'],
                    "cell_1"=>'<input name="delId[]" type="checkbox" value="'.$row_PositionModules['ID'].'" />',
                    "ModuleDir"=>$row_PositionModules['ModuleDir'],
                    "ModuleTitle"=>$row_PositionModules['ModuleTitle'],
                    "module_note"=>$row_PositionModules['ModuleDir'],
                    "cell_5"=>$row_PositionModules['Status']=='true'?'enabled':'disabled',
                    "class_cell"=>$row_PositionModules['Status']=='true'?"cell_groupitem1":"cell_groupitem_ex1",
                );
                $resultHtmlFooter .= $this->khithe_assign($arrBlockHtml['module1'],$arrPositionModuleFooter);
            }


			
		} mysql_free_result($resultPositionModule);


        $active_modules = parent::get_active_modules($strid);
        while( $row = mysql_fetch_assoc($active_modules) )
        {

                $arrPositionModuleMD = array(
                    "ComID"=>$ComID,
                    "cell_ID"=>$row['ID'],
                    "MID"=>$row['ID'],
                    "cell_1"=>'<input name="delId[]" type="checkbox" value="'.$row['ID'].'" />',
                    "ModuleDir"=>$row['ModuleDir'],
                    "ModuleTitle"=>$row['ModuleTitle'],
                    "module_note"=>$row['ModuleDir'],
                    "cell_5"=>$row['Status']=='true'?'enabled':'disabled',
                    "class_cell"=>$row['Status']=='true'?"cell_groupitem1":"cell_groupitem_ex1",
                );
            $resultHtmlModule .= $this->khithe_assign($arrBlockHtml['module1'],$arrPositionModuleMD);
        }



		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array(
            'htmlmodule1'=>$resultHtmlHeader,
            'htmlmodule2'=>$resultHtmlLeft,
            'htmlmodule3'=>$resultHtmlMiddle,
            'htmlmodule4'=>$resultHtmlRight,
            'htmlmodule5'=>$resultHtmlFooter,
            'htmlLeft'=>$resultHtmlModule
        ));
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		return $strHtmlContent;
	}

	public function index_ajax_action(){
		
		switch ($this->valUrl(2))
		{
			case 'change':
			
				return $this->index_action();
				
			break;
			
			case 'drag':
				$arr = explode('----',$this->valUrl(3));
                $com_id =$this->valUrl(1);

                if(!empty($arr)){

                    foreach($arr as $val){
                        $arrv = explode(',',$val);
                        if(!empty($arrv)){
                            $position = str_replace('__0','',$arrv[0]);
                            $_str_id = 0;
                            foreach($arrv as $k=>$v){
                                if($k!=0){

                                    parent::set_pos_mod($position, $v, $com_id, $k);
                                    $_id = parent::get_pos_mod($position, $v, $com_id,true);
                                    $_str_id .= empty($_id)?NULL:','.$_id;
                                }
                            }

                            parent::del_pos_mod($position,$com_id,$_str_id);
                        }

                    }



                }

				return $this->index_action();
				
			break;

			case 'open':

				$arr = explode(',',$this->valUrl(3));
				foreach ( $arr as $ID ){
					$finish = parent::setCheckedPositionModules($ID,'true');
				}
				if($finish)
				return $this->index_action();

			break;

			case 'close':
				$arr = explode(',',$this->valUrl(3));
				foreach ( $arr as $ID ){
					$finish = parent::setCheckedPositionModules($ID,'false');
				}
				if($finish)
				return $this->index_action();
				
			break;
			
			case 'del':
				$arr = explode(',',$this->valUrl(3));
				foreach ( $arr as $ID ){
					$finish = parent::delCheckedPositionModules($ID);
				}
				if($finish)
				return $this->index_action();
				
			break;
			

			
		}	
		
	}
	

}
?>